package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.mapper.EduTeacherMapper;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author Y丶HS练习谷粒商城
 * @since 2022-09-21
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Cacheable( key = "'teacher'",value = "selectTeacher")
    @Override
    public List<EduTeacher> listByLimit4() {
        LambdaQueryWrapper<EduTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(EduTeacher::getId).last( "limit 4");
        List<EduTeacher> selectTeacher = this.list(queryWrapper);
        return selectTeacher;
    }

    @Override
    public Map<String,Object> getTeacherFrontList(Page<EduTeacher> teacherPage ) {
        // 其他条件
        LambdaQueryWrapper<EduTeacher> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.orderByDesc(EduTeacher::getId);
        this.page(teacherPage, queryWrapper);
        // 取出结果
        List records = teacherPage.getRecords();
        // 当前页
        long current = teacherPage.getCurrent();
        // 页大小
        long pages = teacherPage.getPages();
        // 当前页条数
        long total = teacherPage.getTotal();
        // 总条数
        long size = teacherPage.getSize();
        // 是否有下一页
        boolean hasNext = teacherPage.hasNext();
        // 是否有上一页
        boolean hasPrevious = teacherPage.hasPrevious();

        Map<String,Object> map = new HashMap<>();
        map.put("info",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("total",total);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);
        map.put("size",size);
        return map;
    }
}
